//
//  SmsCloudRecordController.m
//  smsCloud
//
//  Created by 蓝鳍互娱 on 2024/7/22.
//

#import "SmsCloudRecordController.h"
#import "SmsCloudMainNavView.h"
#import "SmsCloudRecordHeadView.h"
#import "SmsCloudRecordCell.h"
@interface SmsCloudRecordController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic,strong) SmsCloudMainNavView *smsCloudRecordNavView;
@property (nonatomic,strong) SmsCloudRecordHeadView *smsCloudRecordHeadView;
@property (nonatomic,strong) UITableView *smsCloudRecordTableView;
@property (nonatomic, strong)NSMutableArray * smsCloudRecords;
@end

@implementation SmsCloudRecordController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = YES;
    self.view.backgroundColor = [SmsCloudHead SmsCloudColorHexString:@"#FFFFFF"];
    [self smsCloudInitView];
    self.smsCloudRecords = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"SmsCloudRecord"]];
    if(!self.smsCloudRecords){
        self.smsCloudRecords = [[NSMutableArray alloc] init];
    }
}
- (SmsCloudMainNavView *)smsCloudRecordNavView{
    if(!_smsCloudRecordNavView){
        __weak __typeof__(self) weakSelf = self;
        _smsCloudRecordNavView = [[SmsCloudMainNavView alloc] init];
        _smsCloudRecordNavView.backgroundColor = [SmsCloudHead SmsCloudColorHexString:@"#FFFFFF"];
        _smsCloudRecordNavView.smsCloudNavTitle = @"注册记录";
        _smsCloudRecordNavView.SmsCloudMainBackBlock = ^{
            [weakSelf.navigationController popViewControllerAnimated:true];
        };
    }
    return _smsCloudRecordNavView;
}
- (SmsCloudRecordHeadView *)smsCloudRecordHeadView{
    if(!_smsCloudRecordHeadView){
        _smsCloudRecordHeadView = [[SmsCloudRecordHeadView alloc] init];
    }
    return _smsCloudRecordHeadView;
}
- (UITableView *)smsCloudRecordTableView{
    if(!_smsCloudRecordTableView){
        _smsCloudRecordTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _smsCloudRecordTableView.delegate = self;
        _smsCloudRecordTableView.dataSource = self;
        _smsCloudRecordTableView.backgroundColor = [UIColor clearColor];
        _smsCloudRecordTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _smsCloudRecordTableView;
}
- (void)smsCloudInitView{
    [self.view addSubview:self.smsCloudRecordNavView];
    [self.view addSubview:self.smsCloudRecordHeadView];
    [self.view addSubview:self.smsCloudRecordTableView];

    [self.smsCloudRecordNavView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
        make.right.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
        make.top.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:88] + [SmsCloudHead SmsCloudTopSafeH]));
    }];
    [self.smsCloudRecordHeadView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
        make.right.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
        make.top.equalTo(self.smsCloudRecordNavView.mas_bottom);
        make.height.equalTo(@([SmsCloudHead SmsCloudScreewRatio:148]));
    }];
    [self.smsCloudRecordTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
        make.right.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
        make.top.equalTo(self.smsCloudRecordHeadView.mas_bottom);
        make.bottom.equalTo(@([SmsCloudHead SmsCloudScreewRatio:0]));
    }];
}
-(void)dealloc{
    [NSObject cancelPreviousPerformRequestsWithTarget:self];
}
#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.smsCloudRecords.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [SmsCloudHead SmsCloudScreewRatio:344];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SmsCloudRecordCell *smsCloudRecordCell = [SmsCloudRecordCell smsCloudRecordCell:tableView];
    smsCloudRecordCell.smsCloudDic = [NSMutableDictionary dictionaryWithDictionary:self.smsCloudRecords[indexPath.row]];
    __weak __typeof__(self) weakSelf = self;
    smsCloudRecordCell.SmsCloudRecordPhoneCodeBlock = ^(NSMutableDictionary * _Nonnull smsCloudDic) {
        for (int i = 0; i < weakSelf.smsCloudRecords.count; i++) {
            NSDictionary * smsCloudRecordDic = weakSelf.smsCloudRecords[i];
            if([[smsCloudRecordDic objectForKey:@"phone"] isEqualToString:[smsCloudDic objectForKey:@"phone"]]){
                [weakSelf.smsCloudRecords replaceObjectAtIndex:i withObject:smsCloudDic];
                [[NSUserDefaults standardUserDefaults] setObject:weakSelf.smsCloudRecords.copy forKey:@"SmsCloudRecord"];
            }
        }
    };
    return smsCloudRecordCell;
}
@end
